package com.guodi.map.controller;

import com.guodi.common.constant.StatusCode;
import com.guodi.common.utils.UuidUtil;
import com.guodi.core.boot.ctrl.BaseController;
import com.guodi.core.tool.api.R;
import com.guodi.map.entity.MapParamScheme;
import com.guodi.map.service.IMapParamSchemeService;
import com.guodi.map.service.IMapParamService;
import com.guodi.map.utils.VerificationUtil;
import com.guodi.map.vo.ApiMapParamScheme;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @描述：地图参数方案控制类
 * @作者: 代鹏刚
 * @日期: 2020-05-19
 */
@Validated
@RestController
@RequestMapping("/MapParamScheme")
@Api(value = "API - MapParamSchemeController", tags = "地图参数方案")
public class MapParamSchemeController extends BaseController {

    @Autowired
    public IMapParamSchemeService mapParamSchemeService;
    @Autowired
    private IMapParamService mapParamService;

    /**
    * @描述: 查询列表数据
    * @入参:
    * @出参:
    * @作者: 代鹏刚
    * @日期: 2020-05-19
    **/
    @PostMapping("/listByEntity")
    @ApiOperation(value = " 查询列表数据")
    public R listByEntity(){
        List<MapParamScheme> mapParamSchemes = mapParamSchemeService.list();
        return R.data(mapParamSchemes);
    }

    /**
     * @描述: 根据主键查询
     * @入参:
     * @出参:
     * @作者: 代鹏刚
     * @日期: 2020-05-19
     **/
    @PostMapping("/findById")
    @ApiOperation(value = "根据主键查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true,
                    dataType = "string", paramType = "query")
    })
    public R findById(String id) {
        if(StringUtils.isBlank(id)){
            return R.fail(StatusCode.ERROR_CODE_40005,"id不能为空");
        }
        MapParamScheme mapParamScheme = mapParamSchemeService.getById(id);
        return R.data(mapParamScheme);
    }

    /**
     * @描述: 新增
     * @入参:
     * @出参:
     * @作者: 代鹏刚
     * @日期: 2020-05-19
     **/
    @PostMapping("/save")
    @ApiOperation(value = "新增")
    public R save(@RequestBody  @Valid ApiMapParamScheme apiMapParamScheme){
        MapParamScheme mapParamScheme = new MapParamScheme();
        BeanUtils.copyProperties(apiMapParamScheme,mapParamScheme);
        if(!prepareData(mapParamScheme, true)){
            return R.fail(StatusCode.ERROR_CODE_40005,"该方案名已经存在,请更换！");
        }
        List<MapParamScheme> mapParamSchemes = mapParamSchemeService.list();
        Integer sort = mapParamSchemes.size();
        prepareData(mapParamScheme, true);
        mapParamScheme.setId(UuidUtil.get32UUID());
        mapParamScheme.setSort(sort + 1);
        mapParamSchemeService.save(mapParamScheme);
        return R.data(mapParamScheme);
    }

    /**
     * @描述: 修改
     * @入参:
     * @出参:
     * @作者: 代鹏刚
     * @日期: 2020-05-19
     **/
    @PostMapping("/update")
    @ApiOperation(value = "修改")
    public R update(MapParamScheme mapParamScheme) {
        if(!prepareData(mapParamScheme, false)){
            return R.fail(StatusCode.ERROR_CODE_40005,"该方案名已经存在,请更换！");
        }
        mapParamSchemeService.updateById(mapParamScheme);
        return R.data(mapParamScheme);
    }

    /**
     * @描述: 根据主键删除
     * @入参: id
     * @出参:
     * @作者: 代鹏刚
     * @日期: 2020-05-19
     **/
    @PostMapping("/delete")
    @ApiOperation(value = "根据主键删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true,
                    dataType = "string", paramType = "query")
    })
    public R delete(String id){
        mapParamSchemeService.removeById(id);
        //删除关联地图参数数据
        Map<String, Object> map = new HashMap<>();
        map.put("scheme_id", id);
        mapParamService.removeByMap(map);
        return R.data(null);
    }

    private boolean prepareData(MapParamScheme mapParamScheme, boolean isSave) {
        List<MapParamScheme> mapParamSchemes = (List<MapParamScheme>)mapParamSchemeService.listByMap(VerificationUtil.verifyMap(mapParamScheme.getName(),null));
        if(isSave){
            if(mapParamSchemes.size() > 0){
                return false;
            }
            mapParamScheme.setCreator(getUser().getUserId().toString());
            mapParamScheme.setCreateTime(new Date());
            //防止误修改
            mapParamScheme.setModifier(null);
            mapParamScheme.setModifyTime(null);
            return true;
        }else{
            if(mapParamSchemes.size() > 0 && !mapParamSchemes.get(0).getId().equals(mapParamScheme.getId())){
                return false;
            }
            mapParamScheme.setModifier(getUser().getUserId().toString());
            mapParamScheme.setModifyTime(new Date());
            //防止误修改
            mapParamScheme.setCreator(null);
            mapParamScheme.setCreateTime(null);
            return true;
        }
    }
}
